<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<title>Source code</title>
<link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style">
</head>
<body>
<div class="sourceContainer">
<pre><span class="sourceLineNo">001</span><a name="line.1"></a>
<span class="sourceLineNo">002</span>/*<a name="line.2"></a>
<span class="sourceLineNo">003</span> * Copyright (C) 2011 Archie L. Cobbs. All rights reserved.<a name="line.3"></a>
<span class="sourceLineNo">004</span> *<a name="line.4"></a>
<span class="sourceLineNo">005</span> * $Id: ThreadLocalHolder.html 705 2013-08-28 16:40:23Z archie.cobbs $<a name="line.5"></a>
<span class="sourceLineNo">006</span> */<a name="line.6"></a>
<span class="sourceLineNo">007</span><a name="line.7"></a>
<span class="sourceLineNo">008</span>package org.dellroad.stuff.java;<a name="line.8"></a>
<span class="sourceLineNo">009</span><a name="line.9"></a>
<span class="sourceLineNo">010</span>import java.util.concurrent.Callable;<a name="line.10"></a>
<span class="sourceLineNo">011</span><a name="line.11"></a>
<span class="sourceLineNo">012</span>/**<a name="line.12"></a>
<span class="sourceLineNo">013</span> * Manages a thread local whose lifetime matches the duration of some method call.<a name="line.13"></a>
<span class="sourceLineNo">014</span> *<a name="line.14"></a>
<span class="sourceLineNo">015</span> * &lt;p&gt;<a name="line.15"></a>
<span class="sourceLineNo">016</span> * This class is useful for this common pattern:<a name="line.16"></a>
<span class="sourceLineNo">017</span> * &lt;ul&gt;<a name="line.17"></a>
<span class="sourceLineNo">018</span> *  &lt;li&gt;A thread local variable is instantiated by some initial method call and has an intended<a name="line.18"></a>
<span class="sourceLineNo">019</span> *      lifetime matching the duration of that method call;&lt;/li&gt;<a name="line.19"></a>
<span class="sourceLineNo">020</span> *  &lt;li&gt;The thread local variable is accessible from some other nested method calls in the same thread,<a name="line.20"></a>
<span class="sourceLineNo">021</span> *      as long as the initial method call is still executing;&lt;/li&gt;<a name="line.21"></a>
<span class="sourceLineNo">022</span> *  &lt;li&gt;The thread local variable is removed (and optionally cleaned up) when the initial method call exits,<a name="line.22"></a>
<span class="sourceLineNo">023</span> *      whether successfully or not.&lt;/li&gt;<a name="line.23"></a>
<span class="sourceLineNo">024</span> * &lt;/ul&gt;<a name="line.24"></a>
<span class="sourceLineNo">025</span> * &lt;/p&gt;<a name="line.25"></a>
<span class="sourceLineNo">026</span> *<a name="line.26"></a>
<span class="sourceLineNo">027</span> * &lt;p&gt;<a name="line.27"></a>
<span class="sourceLineNo">028</span> * Example:<a name="line.28"></a>
<span class="sourceLineNo">029</span> * &lt;blockquote&gt;&lt;pre&gt;<a name="line.29"></a>
<span class="sourceLineNo">030</span> * public class Activity {<a name="line.30"></a>
<span class="sourceLineNo">031</span> *<a name="line.31"></a>
<span class="sourceLineNo">032</span> *     private static final ThreadLocalHolder&amp;lt;Activity&amp;gt; CURRENT_ACTIVITY = new ThreadLocalHolder&amp;lt;Activity&amp;gt;();<a name="line.32"></a>
<span class="sourceLineNo">033</span> *<a name="line.33"></a>
<span class="sourceLineNo">034</span> *     public void perform(final Object parameter) {<a name="line.34"></a>
<span class="sourceLineNo">035</span> *         CURRENT_ACTIVITY.invoke(this, new Runnable() {<a name="line.35"></a>
<span class="sourceLineNo">036</span> *             &amp;#64;Override<a name="line.36"></a>
<span class="sourceLineNo">037</span> *             public void run() {<a name="line.37"></a>
<span class="sourceLineNo">038</span> *                 // do whatever with parameter<a name="line.38"></a>
<span class="sourceLineNo">039</span> *             }<a name="line.39"></a>
<span class="sourceLineNo">040</span> *         });<a name="line.40"></a>
<span class="sourceLineNo">041</span> *     }<a name="line.41"></a>
<span class="sourceLineNo">042</span> *<a name="line.42"></a>
<span class="sourceLineNo">043</span> *     /**<a name="line.43"></a>
<span class="sourceLineNo">044</span> *      * Get the current activity being performed.<a name="line.44"></a>
<span class="sourceLineNo">045</span> *      *<a name="line.45"></a>
<span class="sourceLineNo">046</span> *      * @throws IllegalStateException if there is no current activity<a name="line.46"></a>
<span class="sourceLineNo">047</span> *      *&amp;#47;<a name="line.47"></a>
<span class="sourceLineNo">048</span> *     public static Activity currentActivity() {<a name="line.48"></a>
<span class="sourceLineNo">049</span> *         return CURRENT_ACTIVITY.require();<a name="line.49"></a>
<span class="sourceLineNo">050</span> *     }<a name="line.50"></a>
<span class="sourceLineNo">051</span> * }<a name="line.51"></a>
<span class="sourceLineNo">052</span> * &lt;/pre&gt;&lt;/blockquote&gt;<a name="line.52"></a>
<span class="sourceLineNo">053</span> * &lt;/p&gt;<a name="line.53"></a>
<span class="sourceLineNo">054</span> *<a name="line.54"></a>
<span class="sourceLineNo">055</span> * @param &lt;T&gt; the type of the thread local variable<a name="line.55"></a>
<span class="sourceLineNo">056</span> */<a name="line.56"></a>
<span class="sourceLineNo">057</span>public class ThreadLocalHolder&lt;T&gt; {<a name="line.57"></a>
<span class="sourceLineNo">058</span><a name="line.58"></a>
<span class="sourceLineNo">059</span>    private final ThreadLocal&lt;T&gt; threadLocal;<a name="line.59"></a>
<span class="sourceLineNo">060</span><a name="line.60"></a>
<span class="sourceLineNo">061</span>    /**<a name="line.61"></a>
<span class="sourceLineNo">062</span>     * Conveninece constructor.<a name="line.62"></a>
<span class="sourceLineNo">063</span>     *<a name="line.63"></a>
<span class="sourceLineNo">064</span>     * &lt;p&gt;<a name="line.64"></a>
<span class="sourceLineNo">065</span>     * Equivalent to:<a name="line.65"></a>
<span class="sourceLineNo">066</span>     *  &lt;blockquote&gt;&lt;code&gt;new ThreadLocalHolder&amp;lt;T&amp;gt;(new ThreadLocal&amp;lt;T&amp;gt;())&lt;/code&gt;&lt;/blockquote&gt;<a name="line.66"></a>
<span class="sourceLineNo">067</span>     */<a name="line.67"></a>
<span class="sourceLineNo">068</span>    public ThreadLocalHolder() {<a name="line.68"></a>
<span class="sourceLineNo">069</span>        this(new ThreadLocal&lt;T&gt;());<a name="line.69"></a>
<span class="sourceLineNo">070</span>    }<a name="line.70"></a>
<span class="sourceLineNo">071</span><a name="line.71"></a>
<span class="sourceLineNo">072</span>    /**<a name="line.72"></a>
<span class="sourceLineNo">073</span>     * Primary constructor.<a name="line.73"></a>
<span class="sourceLineNo">074</span>     *<a name="line.74"></a>
<span class="sourceLineNo">075</span>     * @param threadLocal the thread local to use<a name="line.75"></a>
<span class="sourceLineNo">076</span>     * @throws IllegalArgumentException if {@code threadLocal} is null<a name="line.76"></a>
<span class="sourceLineNo">077</span>     */<a name="line.77"></a>
<span class="sourceLineNo">078</span>    public ThreadLocalHolder(ThreadLocal&lt;T&gt; threadLocal) {<a name="line.78"></a>
<span class="sourceLineNo">079</span>        if (threadLocal == null)<a name="line.79"></a>
<span class="sourceLineNo">080</span>            throw new IllegalArgumentException("null threadLocal");<a name="line.80"></a>
<span class="sourceLineNo">081</span>        this.threadLocal = threadLocal;<a name="line.81"></a>
<span class="sourceLineNo">082</span>    }<a name="line.82"></a>
<span class="sourceLineNo">083</span><a name="line.83"></a>
<span class="sourceLineNo">084</span>    /**<a name="line.84"></a>
<span class="sourceLineNo">085</span>     * Invoke the given action while making the given thread local variable available via {@link #get} and {@link #require}.<a name="line.85"></a>
<span class="sourceLineNo">086</span>     *<a name="line.86"></a>
<span class="sourceLineNo">087</span>     * &lt;p&gt;<a name="line.87"></a>
<span class="sourceLineNo">088</span>     * If there is already a thread local variable set for the current thread (i.e., we are already executing within<a name="line.88"></a>
<span class="sourceLineNo">089</span>     * an invocation of &lt;code&gt;ThreadLocalHolder.invoke()&lt;/code&gt;), then if {@code value} is the exact same Java object<a name="line.89"></a>
<span class="sourceLineNo">090</span>     * (using object equality, not &lt;code&gt;equals()&lt;/code&gt;), execution proceeds normally, otherwise an exception is thrown.<a name="line.90"></a>
<span class="sourceLineNo">091</span>     *<a name="line.91"></a>
<span class="sourceLineNo">092</span>     * @param value value for the thread local variable<a name="line.92"></a>
<span class="sourceLineNo">093</span>     * @param action action to invoke<a name="line.93"></a>
<span class="sourceLineNo">094</span>     * @throws IllegalArgumentException if either {@code action} or {@code value} is null<a name="line.94"></a>
<span class="sourceLineNo">095</span>     * @throws IllegalStateException if there is already a thread local variable &lt;code&gt;previous&lt;/code&gt;<a name="line.95"></a>
<span class="sourceLineNo">096</span>     *  associated with the current thread and &lt;code&gt;value != previous&lt;/code&gt;<a name="line.96"></a>
<span class="sourceLineNo">097</span>     */<a name="line.97"></a>
<span class="sourceLineNo">098</span>    public void invoke(final T value, Runnable action) {<a name="line.98"></a>
<span class="sourceLineNo">099</span>        if (action == null)<a name="line.99"></a>
<span class="sourceLineNo">100</span>            throw new IllegalArgumentException("null action");<a name="line.100"></a>
<span class="sourceLineNo">101</span>        if (value == null)<a name="line.101"></a>
<span class="sourceLineNo">102</span>            throw new IllegalArgumentException("null value");<a name="line.102"></a>
<span class="sourceLineNo">103</span>        final T previousValue = this.threadLocal.get();<a name="line.103"></a>
<span class="sourceLineNo">104</span>        final boolean topLevel = previousValue == null;<a name="line.104"></a>
<span class="sourceLineNo">105</span>        if (!topLevel) {<a name="line.105"></a>
<span class="sourceLineNo">106</span>            if (value != previousValue) {<a name="line.106"></a>
<span class="sourceLineNo">107</span>                throw new IllegalStateException("already executing within an invocation of ThreadLocalHolder.invoke()"<a name="line.107"></a>
<span class="sourceLineNo">108</span>                  + " but with a different value");<a name="line.108"></a>
<span class="sourceLineNo">109</span>            }<a name="line.109"></a>
<span class="sourceLineNo">110</span>        } else<a name="line.110"></a>
<span class="sourceLineNo">111</span>            this.threadLocal.set(value);<a name="line.111"></a>
<span class="sourceLineNo">112</span>        try {<a name="line.112"></a>
<span class="sourceLineNo">113</span>            action.run();<a name="line.113"></a>
<span class="sourceLineNo">114</span>        } finally {<a name="line.114"></a>
<span class="sourceLineNo">115</span>            if (topLevel) {<a name="line.115"></a>
<span class="sourceLineNo">116</span>                this.threadLocal.remove();<a name="line.116"></a>
<span class="sourceLineNo">117</span>                this.destroy(value);<a name="line.117"></a>
<span class="sourceLineNo">118</span>            }<a name="line.118"></a>
<span class="sourceLineNo">119</span>        }<a name="line.119"></a>
<span class="sourceLineNo">120</span>    }<a name="line.120"></a>
<span class="sourceLineNo">121</span><a name="line.121"></a>
<span class="sourceLineNo">122</span>    /**<a name="line.122"></a>
<span class="sourceLineNo">123</span>     * Invoke the given action while making the given thread local variable available via {@link #get} and {@link #require}.<a name="line.123"></a>
<span class="sourceLineNo">124</span>     *<a name="line.124"></a>
<span class="sourceLineNo">125</span>     * &lt;p&gt;<a name="line.125"></a>
<span class="sourceLineNo">126</span>     * If there is already a thread local variable set for the current thread (i.e., we are already executing within<a name="line.126"></a>
<span class="sourceLineNo">127</span>     * an invocation of &lt;code&gt;ThreadLocalHolder.invoke()&lt;/code&gt;), then if {@code value} is the exact same Java object<a name="line.127"></a>
<span class="sourceLineNo">128</span>     * (using object equality, not &lt;code&gt;equals()&lt;/code&gt;), execution proceeds normally, otherwise an exception is thrown.<a name="line.128"></a>
<span class="sourceLineNo">129</span>     *<a name="line.129"></a>
<span class="sourceLineNo">130</span>     * @param value value for the thread local variable<a name="line.130"></a>
<span class="sourceLineNo">131</span>     * @param action action to invoke<a name="line.131"></a>
<span class="sourceLineNo">132</span>     * @throws IllegalArgumentException if either {@code action} or {@code value} is null<a name="line.132"></a>
<span class="sourceLineNo">133</span>     * @throws IllegalStateException if there is already a thread local variable &lt;code&gt;previous&lt;/code&gt;<a name="line.133"></a>
<span class="sourceLineNo">134</span>     *  associated with the current thread and &lt;code&gt;value != previous&lt;/code&gt;<a name="line.134"></a>
<span class="sourceLineNo">135</span>     * @throws Exception if {@code action} throws an {@link Exception}<a name="line.135"></a>
<span class="sourceLineNo">136</span>     */<a name="line.136"></a>
<span class="sourceLineNo">137</span>    public &lt;R&gt; R invoke(final T value, Callable&lt;R&gt; action) throws Exception {<a name="line.137"></a>
<span class="sourceLineNo">138</span>        if (action == null)<a name="line.138"></a>
<span class="sourceLineNo">139</span>            throw new IllegalArgumentException("null action");<a name="line.139"></a>
<span class="sourceLineNo">140</span>        if (value == null)<a name="line.140"></a>
<span class="sourceLineNo">141</span>            throw new IllegalArgumentException("null value");<a name="line.141"></a>
<span class="sourceLineNo">142</span>        final T previousValue = this.threadLocal.get();<a name="line.142"></a>
<span class="sourceLineNo">143</span>        final boolean topLevel = previousValue == null;<a name="line.143"></a>
<span class="sourceLineNo">144</span>        if (!topLevel) {<a name="line.144"></a>
<span class="sourceLineNo">145</span>            if (value != previousValue) {<a name="line.145"></a>
<span class="sourceLineNo">146</span>                throw new IllegalStateException("already executing within an invocation of ThreadLocalHolder.invoke()"<a name="line.146"></a>
<span class="sourceLineNo">147</span>                  + " but with a different value");<a name="line.147"></a>
<span class="sourceLineNo">148</span>            }<a name="line.148"></a>
<span class="sourceLineNo">149</span>        } else<a name="line.149"></a>
<span class="sourceLineNo">150</span>            this.threadLocal.set(value);<a name="line.150"></a>
<span class="sourceLineNo">151</span>        try {<a name="line.151"></a>
<span class="sourceLineNo">152</span>            return action.call();<a name="line.152"></a>
<span class="sourceLineNo">153</span>        } finally {<a name="line.153"></a>
<span class="sourceLineNo">154</span>            if (topLevel) {<a name="line.154"></a>
<span class="sourceLineNo">155</span>                this.threadLocal.remove();<a name="line.155"></a>
<span class="sourceLineNo">156</span>                this.destroy(value);<a name="line.156"></a>
<span class="sourceLineNo">157</span>            }<a name="line.157"></a>
<span class="sourceLineNo">158</span>        }<a name="line.158"></a>
<span class="sourceLineNo">159</span>    }<a name="line.159"></a>
<span class="sourceLineNo">160</span><a name="line.160"></a>
<span class="sourceLineNo">161</span>    /**<a name="line.161"></a>
<span class="sourceLineNo">162</span>     * Get the thread local value associated with the current thread, if any.<a name="line.162"></a>
<span class="sourceLineNo">163</span>     *<a name="line.163"></a>
<span class="sourceLineNo">164</span>     * @return the current thread local variable value, or null if not executing<a name="line.164"></a>
<span class="sourceLineNo">165</span>     *  within an invocation of &lt;code&gt;ThreadLocalHolder.invoke()&lt;/code&gt;<a name="line.165"></a>
<span class="sourceLineNo">166</span>     */<a name="line.166"></a>
<span class="sourceLineNo">167</span>    public T get() {<a name="line.167"></a>
<span class="sourceLineNo">168</span>        return this.threadLocal.get();<a name="line.168"></a>
<span class="sourceLineNo">169</span>    }<a name="line.169"></a>
<span class="sourceLineNo">170</span><a name="line.170"></a>
<span class="sourceLineNo">171</span>    /**<a name="line.171"></a>
<span class="sourceLineNo">172</span>     * Get the thread local value associated with the current thread; there must be one.<a name="line.172"></a>
<span class="sourceLineNo">173</span>     *<a name="line.173"></a>
<span class="sourceLineNo">174</span>     * @return the current thread local variable value, never null<a name="line.174"></a>
<span class="sourceLineNo">175</span>     * @throws IllegalStateException if the current thread is not running<a name="line.175"></a>
<span class="sourceLineNo">176</span>     *  within an invocation of &lt;code&gt;ThreadLocalHolder.invoke()&lt;/code&gt;<a name="line.176"></a>
<span class="sourceLineNo">177</span>     */<a name="line.177"></a>
<span class="sourceLineNo">178</span>    public T require() {<a name="line.178"></a>
<span class="sourceLineNo">179</span>        T value = this.threadLocal.get();<a name="line.179"></a>
<span class="sourceLineNo">180</span>        if (value == null) {<a name="line.180"></a>
<span class="sourceLineNo">181</span>            throw new IllegalStateException("no value associated with the current thread;"<a name="line.181"></a>
<span class="sourceLineNo">182</span>              + " are we executing within an invocation of ThreadLocalHolder.invoke()?");<a name="line.182"></a>
<span class="sourceLineNo">183</span>        }<a name="line.183"></a>
<span class="sourceLineNo">184</span>        return value;<a name="line.184"></a>
<span class="sourceLineNo">185</span>    }<a name="line.185"></a>
<span class="sourceLineNo">186</span><a name="line.186"></a>
<span class="sourceLineNo">187</span>    /**<a name="line.187"></a>
<span class="sourceLineNo">188</span>     * Clean up the thread local value when no longer needed.<a name="line.188"></a>
<span class="sourceLineNo">189</span>     *<a name="line.189"></a>
<span class="sourceLineNo">190</span>     * &lt;p&gt;<a name="line.190"></a>
<span class="sourceLineNo">191</span>     * The implementation in {@link ThreadLocalHolder} does nothing. Subclasses may override if necessary.<a name="line.191"></a>
<span class="sourceLineNo">192</span>     */<a name="line.192"></a>
<span class="sourceLineNo">193</span>    protected void destroy(T value) {<a name="line.193"></a>
<span class="sourceLineNo">194</span>    }<a name="line.194"></a>
<span class="sourceLineNo">195</span>}<a name="line.195"></a>
<span class="sourceLineNo">196</span><a name="line.196"></a>




























































</pre>
</div>
</body>
</html>
